Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience finder methods by uuid and handle #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

silviucm
Copy link

@silviucm silviucm commented Mar 18, 2018

This pull request adds several convenience methods for identifying services, characteristics or descriptors, based on their uuid or handle, in addition to the original Find method.

// FindWithUUID searches discovered profile for the specified UUID.
// The target must has the type of *Service, *Characteristic, or *Descriptor.
func (p *Profile) FindWithUUID(uuidVal UUID) interface{}

// FindByHandle searches discovered profile for the specified target's type and handle.
// The target must has the type of *Service, *Characteristic, or *Descriptor.
// It also must have the Handle property set to the the right value.
//
// Example:
//	dummyCharacteristic := ble.NewCharacteristic((ble.UUID)(nil))
//	dummyCharacteristic.Handle = curr.handle
//	if u := curr.profile.FindByHandle(dummyCharacteristic); u != nil {
//	...
//	}
func (p *Profile) FindByHandle(target interface{}) interface{}

// FindWithHandle searches the discovered profile for the item the matches
// the given handle, in the following order: services, characteristics, descriptors.
//
// Example:
//	if u := curr.profile.FindWithHandle(1024); u != nil {
//	...
//	}
func (p *Profile) FindWithHandle(handle uint16) interface{}

// FindServiceWithHandle searches discovered profile to find any matching *Service
// for the specified handle. It returns nil if none found.
func (p *Profile) FindServiceWithHandle(handle uint16) *Service
{ ... }

// FindCharacteristicWithHandle searches discovered profile to find any
// matching *Characteristic for the specified handle. It returns nil if none found.
func (p *Profile) FindCharacteristicWithHandle(handle uint16) *Characteristic
{ ... }

// FindDescriptorWithHandle searches discovered profile to find any
// matching *Descriptor for the specified handle. It returns nil if none found.
func (p *Profile) FindDescriptorWithHandle(handle uint16) *Descriptor
{ ... }

// FindWithHandleStr searches the discovered profile for the item whose handle
// matches the handleHexOrDec string value, with an optional base specified.
//
// When handleHexOrDec is converted to uint16, and is prefixed by "0x", it is
// assumed to be in base 16; otherwise, it is assumed to be in base 10, unless the
// optional base argument is explicitly passed.
// The search is performedin the following order: services, characteristics, descriptors.
//
// Example using a hexadecimal value
//	if u, err := curr.profile.FindWithHandleStr("0x10B4"); u != nil {
//	...
//	}
// Example using a decimal value
//	if u, err := curr.profile.FindWithHandleStr("2048"); u != nil {
//	...
//	}
// Example using a decimal value with explicit base 10
//	if u, err := curr.profile.FindWithHandleStr("745", 10); u != nil {
//	...
//	}
func (p *Profile) FindWithHandleStr(handleStr string, base ...int) (r interface{}, err error)

@silviucm
Copy link
Author

Hi @roylee17 - when you have a chance could you review this ? if approved, I would go on and fix the write part of the blesh tool

@roylee17
Copy link
Contributor

roylee17 commented Mar 27, 2018

@silviucm sorry for the late response.

Thanks for the input.
The implementation looks good to me if these are internal utility functions.
But, since these broaden the API too much, do you want to take on another direction?

Something like how Sort package works.

  1. Define the finder interface
  2. Add just one API to take the finder as input
  3. Add few common/default finders so user can grab and use, or define their own criteria.

// (details or naming are left out)

type interface xxFinder {
  Find( XXX ) interface {}
}

type ByHandle struct { }

func (f *ByHandle) Find ( XXX ) interface{} {
}

type ByUUID struct { }

func (f *ByUUID) Find ( XXX ) interface{} {
}
func FindBy( f xxFinder) interface {} {

   // Traverse svcs, chars, descs, ... and see if f apply

}

@silviucm
Copy link
Author

@roylee17 fair enough. Changes updated

@roylee17
Copy link
Contributor

LGTM, thanks.

@silviucm
Copy link
Author

@roylee17 if you could approve this merge at your convenience, I could get going on the second part, fixing the blesh tool write part + a bit of documentation for it

thanks

@roylee17
Copy link
Contributor

ping @hasty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants